<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" class="msg"/>
    <button class="submit">发送</button>
    <script>
        let ws = new WebSocket('ws://localhost:9974');
        let msg = document.querySelector('.msg');
        let submit = document.querySelector('.submit');

        ws.onopen  = (res)=>{
            console.log('连接已建立',res);
        }

        ws.onmessage = (msg)=>{
            makeMsg(msg.data);
        }

        submit.onclick = ()=>{
            ws.send(msg.value);
        }

        function makeMsg(data){
            let div = document.createElement('div');
            div.innerHTML = data;
            document.body.appendChild(div);
        }
    </script>
</body>
</html>